/*
自己选择的路，跪着也要走完。朋友们 ，虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 ，不管其它人怎么样,我
们也能够保持自己的本色走下去。                      
To the world , you will be a person , but to a person , you 
will be the world .                               ——AKPower
*/
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
map<char, ll> mp;
map<ll, char> r_mp;
stack<char> st;
stack<double> st_num;
string S[10000];
bool flag[100000];
ll ans;

ll ch(string s_num)
{
    ll len = s_num.length();
    ll num = 0;
    for (ll i = 0; i < len; i++)
    {
        num = num * 10 + (s_num[i] - '0');
    }
    return num;
}

int main()
{
    // IOS;
    mp['*'] = mp['/'] = 3;
    mp['+'] = mp['-'] = 2;
    mp['('] = 1;
    string s; //表达式
    while (true)
    {
        memset(flag, false, sizeof(flag));
        cout << "1.负数不支持,请使用(0-x)形式表示表示负数" << endl;
        cout << "2.只支持整数运算（结果可以是小数）" << endl;
        cout << "输入要计算的表达式(输入"
             << "exit"
             << ")表示退出:" << endl;
        cin >> s;
        ll S_index = 0;
        if (s == "exit")
            return 0;
        ll len = s.length();
        string temp;
        // cout<<"*********(转后缀表达式)********"<<endl;
        for (ll i = 0; i < len; i++)
        {
            if (s[i] == '(')
            {
                // cout<<"比较("<<endl;
                st.push(s[i]);
                continue;
            }
            if (s[i] == ')')
            {
                // cout<<"比较)"<<endl;
                while (st.top() != '(')
                {
                    temp = "";
                    temp.push_back(st.top());
                    st.pop();
                    S[S_index++] = temp;
                }
                st.pop();
                continue;
            }
            if (s[i] >= '0' && s[i] <= '9')
            {
                // cout<<"比较number"<<endl;
                temp = "";
                while (i < len && s[i] >= '0' && s[i] <= '9')
                {
                    temp.push_back(s[i]);
                    i++;
                }
                i--;
                S[S_index++] = temp;
                continue;
            }
            // cout<<"比较运算符"<<endl;
            while (!st.empty() && mp[st.top()] >= mp[s[i]])
            {
                temp = "";
                temp.push_back(st.top());
                st.pop();
                S[S_index++] = temp;
            }
            st.push(s[i]);
        }
        while (!st.empty())
        {
            temp = "";
            temp.push_back(st.top());
            st.pop();
            S[S_index++] = temp;
        }
        // cout<<"*********(后缀表达式求值)********"<<endl;
        bool isfalse = false;
        for (ll i = 0; i < S_index; i++)
        {
            temp = S[i];
            if (temp[0] >= '0' && temp[0] <= '9')
            {
                st_num.push(ch(temp));
                continue;
            }
            double num1, num2;
            if (temp == "+")
            {
                num1 = st_num.top();
                st_num.pop();
                num2 = st_num.top();
                st_num.pop();
                st_num.push(num1 + num2);
                continue;
            }
            if (temp == "-")
            {
                num1 = st_num.top();
                st_num.pop();
                num2 = st_num.top();
                st_num.pop();
                st_num.push(num2 - num1);
                continue;
            }
            if (temp == "*")
            {
                num1 = st_num.top();
                st_num.pop();
                num2 = st_num.top();
                st_num.pop();
                st_num.push(num2 * num1);
                continue;
            }
            if (temp == "/")
            {
                num1 = st_num.top();
                st_num.pop();
                num2 = st_num.top();
                st_num.pop();
                if (num1 == 0)
                {
                    isfalse = true;
                    break;
                }
                st_num.push(num2 / num1);
                continue;
            }
        }
        if (isfalse)
        {
            while (!st_num.empty())
                st_num.pop();
            cout << "除数为零，出错！" << endl;
        }
        else
        {
            cout << "the ans = " << st_num.top() << endl;
            st_num.pop();
        }
    }

    getchar();
    getchar();
    return 0;
}
